package Controller;

import java.awt.Frame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.IOException;
import java.sql.SQLException;

import javax.swing.JFrame;
import javax.swing.table.TableModel;

import Entity.Employee;
import Model.EmployeeManageModel;
import Model.LocationManageModel;
import View.EmployeeManageView;
import View.LocationManageView;

public class EmployeeManageController {
	private EmployeeManageView view = null;
	private EmployeeManageModel model = null;
	private JFrame frame;
	
	public EmployeeManageController() {
		super();
		frame = new JFrame();
		view = new EmployeeManageView();
		try {
			model = new EmployeeManageModel();
			frame.add(view);
			view.setModel(model);
			doAction();		
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		view.setEnableControl(false);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setSize(800, 600);
		frame.setVisible(true);
	}

	private void doAction() {
		doAdd();
		doEdit();
		doDelete();
		doWrite();
		doSelectTable();
	}
	
	private void doSelect() {
		int row = view.getSelectedItem();
		Employee employee = model.getEmployeeList().get(row);
		view.display(employee);
		view.setEnableControl(false);
	}
	
	private void doSelectTable() {
		view.doSelectTable(new KeyAdapter() {

			public void keyReleased(KeyEvent e) {
				doSelect();
			}
		});

		view.doSelectTable(new MouseAdapter() {

			public void mouseClicked(MouseEvent e) {
				doSelect();
			}
		});
	}

	private void doDelete() {
		view.doDelete(new ActionListener() {

			public void actionPerformed(ActionEvent e) {
				int row = view.getSelectedItem();
				//model.deleteEmployee(employee);
			}
		});
	}

	private void doEdit() {
		view.doEdit(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				view.setEnableControl(true);
			}
		});
	}

	private void doAdd() {
		view.doAdd(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				view.display(new Employee("", "", "", "", "", "", "", "", "", "", "", "", ""));
				view.setEnableControl(true);
			}

		});
	}
	
	private void doWrite() {
		view.doWrite(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
			}
			
		});
	}
	
	public static void main(String[] args) {
		new EmployeeManageController();
	}
}
